package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.Tag;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.TagMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    //关联mapper
    @Autowired
    private StrategyCommentMapper strategyCommentMapper;
    //关联mapper
    @Autowired
    private TagMapper tagMapper;

    @Override
    public void saveOrUpdate(StrategyComment strategyComment, String[] tags) {
        //id为空,新增操作
        if(strategyComment.getId()==null){
        //设置当前创建时间
        strategyComment.setCreateTime(new Date());
         strategyCommentMapper.insert(strategyComment);
        //遍历所有标签
        for (String ele : tags) {
            Tag tag = new Tag();
            tag.setName(ele);
            tagMapper.insert(tag);
            //保存到中间表
            strategyCommentMapper.insertRelation(strategyComment.getId(),tag.getId());
        }
        }else{
            strategyCommentMapper.updateByPrimaryKey(strategyComment);
        }
    }

    @Override
    public PageInfo<StrategyComment> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderby());
        List<StrategyComment> list = strategyCommentMapper.selectForList(qo);
        return new PageInfo<StrategyComment>(list);
    }



}
